// pages/user/personVerify/personVerify.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    applyList: [], //保存所有申请的数组
    page: 0, //分页 第一页
    totalCount: 0, //数据库中总的记录数
    pageSizes: 5, //每页显示5条记录
    haveData: false, //控制页面底部 “已显示全部数据”
    falseImg: "images/publish/none.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    //页面刷新，恢复初始设置
    this.setData({ page: 0, applyList: [], haveData: false });
    const status = await this.getTotalCount();
    if (status == "ok") {
      //获取所有接单申请
      await this.getApplyList();
    } else {
      wx.showToast({ title: "服务器错误", icon: "error" });
      return;
    }
  },
  //获取数据库记录数
  getTotalCount() {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${app.globalData.server}/wxApplyCount`,
        success: (res) => {
          if (res.data.status === "success") {
            this.setData({ totalCount: res.data.count });
            resolve("ok");
          } else {
            reject("err");
          }
        },
        fail: (err) => {
          reject(err);
        },
      });
    });
  },
  //读取用户的 接单申请
  getApplyList() {
    return new Promise((resolve, reject) => {
      const { applyList, page, pageSizes, totalCount } = this.data;
      //applyList.length为已取得的数据记录数，>=totalCount 说明已取完所有数据或不存 在数据
      if (applyList.length >= totalCount) {
        this.setData({ haveData: false });
        return;
      }
      wx.showLoading({ title: "加载中" });
      wx.request({
        url: `${app.globalData.server}/wxGetAllApply`,
        data: {
          page: page * pageSizes,
          //分页读取
          pageSizes,
          //读取记录条数
        },
        success: (res) => {
          //console.log(res)
          if (res.data.length > 0) {
            const { applyList } = this.data;
            this.setData({ applyList: applyList.concat(res.data) });
            //console.log(this.data.applyList)
            resolve({ status: "ok" });
          }
          wx.hideLoading();
        },
        fail: (err) => {
          wx.hideLoading();
          console.log(err);
          reject(err);
        },
      });
    });
  },
  //审核 通过审核/未通过审核
  agree(e) {
    // console.log(e)
    const { id, status } = e.currentTarget.dataset;
    wx.request({
      url: `${app.globalData.server}/wxApplyVerify`,
      method: "POST",
      data: { _id: id, status },
      success: (res) => {
        if (res.data == "success") {
          wx.showToast({ title: "操作成功", icon: "success" });
          setTimeout(() => {
            this.onLoad();
          }, 1000);
        } else {
          wx.showToast({ title: "操作失败", icon: "error" });
        }
      },
      fail: (err) => {
        console.log(err);
        wx.showToast({ title: "操作失败", icon: "error" });
      },
    });
  },
  //审核 通过审核/未通过审核
  disagree(e) {
    // console.log(e)
    const { id, status } = e.currentTarget.dataset;
    wx.request({
      url: `${app.globalData.server}/wxApplyVerify`,
      method: "POST",
      data: { _id: id, status },
      success: (res) => {
        if (res.data == "success") {
          wx.showToast({ title: "操作成功", icon: "success" });
          setTimeout(() => {
            this.onLoad();
          }, 1000);
        } else {
          wx.showToast({ title: "操作失败", icon: "error" });
        }
      },
      fail: (err) => {
        console.log(err);
        wx.showToast({ title: "操作失败", icon: "error" });
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    wx.showLoading();
    this.setData({ page: 0, applyList: [], haveData: false });
    const status = await this.getTotalCount();
    if (status == "ok") {
      //获取所有接单申请
      await this.getApplyList();
    } else {
      wx.showToast({ title: "服务器错误", icon: "error" });
      return;
    }
    wx.hideLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    this.data.page += 1;
    await this.getApplyList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
